<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>问答-登录页面</title>
  <style>
/*样式代码 */
body {
background-color: #f2f2f2;
font-family:Arial,Helvetica,sans-serif;
}
.container {
width: 400px;
margin:0 auto;
padding: 20px;
background-color: #fff;
border-radius:5px;
box-shadow:0px 10px rgba(0,0,0,0.1);
}
.hl {
text-align: center;
margin-bottom:20px;
}
.label {
  display: block;
  margin-bottom:10px;
}
input[type="text"],
input[type="password"]{
  width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
margin-bottom: 20px;
}

button{
background-color: #1B4AEF;
color: #fff;
border: none;
padding: 10px 20px;
border-radius:5px;
cursor: pointer;
}
  </style>
</head>

<body>
  <div class="container">
    <h1>登录</h1>
    <form>
    <label for="username">用户名:</label>
    <input type="text" id="username" name="username" required>
    <button class="login-btn">登录</button>
     </form>
    </div>
</body>
<script>
  const loginButton = document.querySelector(".login-btn")
  const userNameInput = document.querySelector("input[name='username']")
  loginButton.addEventListener("click",handleLogin)

  function handleLogin(e){
    e.preventDefault()
    const userName = userNameInput.value
    if(userName === null || userName === ""){
      return
    }
    const data = {
      userName
    }
    login(data)

  }

  function login(data){
    fetch("/user/login",{
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify(data),
    }).then(response => {
      return response.json()
    }).then(data => {
      console.log(data)
      localStorage.setItem("userInfo",JSON.stringify(data.data))
      location.href = "chat.html"
    });
  }
</script>
</html>